<?php
/**
 * 站群管理
 * @author cjli
 *
 */
class ClusterAction extends AdminAction
{
	//站群列表
	public function cluster_list()
	{
		$cluster_db = D('cluster');
		$cluster_db->field('id,apiKey,username,phone,mobile,company_name,enabled,reg_time');
		$clusterList = $cluster_db->select();
		$this->assign('clusterList', $clusterList);
		
		$this->display('cluster_list');
	}
	
	//添加子站
	public function cluster_add()
	{
		if(isset($_POST['dosubmit'])){
			$_POST['reg_time'] = time();
			import('@.ORG.String');
			$_POST['apiKey'] = String::randString(32);
			$cluster_db = D('cluster');
			$cluster_db->create();
			$cid = $cluster_db->add();
			if($cid){
				S('cluster_list', NULL);
				$this->success('操作成功！', U('cluster/cluster_list'));
			}else{
				$this->error('操作失败！', U('cluster/cluster_list'));
			}
		}else{
			$this->display('cluster_form');
		}
	}
	//编辑子站
	public function cluster_edit()
	{
		$cid = intval($_GET['cid']);
		$cluster_db = D('cluster');
		$cluster = $cluster_db->where('id='.$cid)->find();
		if(!$cluster){
			$this->error('子站不存在！', U('cluster/cluster_list'));
		}
		
		if(isset($_POST['dosubmit'])){
			$cluster_db->create();
			$cid = $cluster_db->where('id='.$cid)->save();
			S('cluster_list', NULL);
			if($cid){
				$this->success('操作成功！', U('cluster/cluster_list'));
			}else{
				$this->error('操作失败！', U('cluster/cluster_list'));
			}
		}else{
			$this->assign('cluster', $cluster);
			$this->display('cluster_form');
		}
	}
	
	//删除子站点
	public function cluster_delete()
	{
		$cid = intval($_GET['cid']);
		$cluster_db = D('cluster');
		$cluster = $cluster_db->where('id='.$cid)->find();
		if(!$cluster){
			$this->error('子站不存在！', U('cluster/cluster_list'));
		}
		$cluster_db->delete($cid);
		S('cluster_list', NULL);
		$this->success('操作成功！', U('cluster/cluster_list'));
	}
}